package com.example.h1nson.realphotography.asyncTask;

import android.os.AsyncTask;

import com.example.h1nson.realphotography.config.ConfigURL;
import com.example.h1nson.realphotography.config.GetCurrentUser;
import com.example.h1nson.realphotography.entity.User;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;
import com.example.h1nson.realphotography.util.HttpClientPost;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by H1nson on 2016/4/28.
 */
public class LoginAsynTask extends AsyncTask<String, Integer, String> {
    private User user;
    private OnDataFinishedListener onDataFinishedListener;
    public LoginAsynTask(User user) {
        super();
        this.user = user;
    }
    public void setOnDataFinishedListener(
            OnDataFinishedListener onDataFinishedListener) {
        this.onDataFinishedListener = onDataFinishedListener;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {
        if(s!=null){

            try {
                JSONObject json = new JSONObject(s);
                String res = json.getString("result");
                if(res.toString().equals("success")){
                    User user = new User(json.getInt("userId"),json.getString("userName"),json.getString("email"),json.getString("password"),
                            json.getString("userInfo"),json.getString("userPreferAddress"),json.getString("userHead"));
                    GetCurrentUser.currentUser = user;
                }else if(res.toString().equals("successWithMqtt")){
                    User user = new User(json.getInt("userId"),json.getString("userName"),json.getString("email"),json.getString("password"),
                            json.getString("userInfo"),json.getString("userPreferAddress"),json.getString("userHead"));
                    GetCurrentUser.currentUser = user;
                }
                onDataFinishedListener.onDataSuccessfully(res);
            }catch (JSONException e){
                e.printStackTrace();
            }
        }else{
            onDataFinishedListener.onDataFailed();
        }
        super.onPostExecute(s);

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled(String s) {
        super.onCancelled(s);
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected String doInBackground(String... params) {
       // String jsonStr = jsonReq();//"{\"userName\":\"舒新胜\",\"password\":\"78787\",\"email\":\"383838@qq.com\"}";
        String jsonStr = this.jsonReq();
        HttpClientPost post = new HttpClientPost(ConfigURL.loginURL,jsonStr);
        post.doPost();
        return post.getJsonStrRes();
    }
    public String jsonReq(){
        JSONObject jsonObject = new JSONObject();
        try{
           // jsonObject.put("userName",user.getUserName());
            jsonObject.put("password", user.getUserPassword());
            jsonObject.put("email", user.getUserEmail());
        }catch (JSONException e){
            e.printStackTrace();
        }
        return jsonObject.toString();
    }
}
